package com.zjw.service.impl;

import com.zjw.dao.NoteDao;
import com.zjw.dao.impl.NoteDaoImpl;
import com.zjw.entity.Note;
import com.zjw.service.NoteService;
import com.zjw.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @projectName: note
 * @package: com.zjw.service.impl
 * @className: NoteServiceImpl
 * @author: 翟
 * @date: 2025/4/3 14:17
 */

public class NoteServiceImpl implements NoteService {


    @Override
    public boolean addNote(Note note) {
        Connection conn = ConnectionUtil.getConnection();
        NoteDao noteDao = new NoteDaoImpl(conn);
        int num = 0;
        try {
            num = noteDao.insertNote(note);

        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("数据库服务出错了！！！");
        } finally {
            ConnectionUtil.closeALL(null, null, conn);
        }
//        if (num == 1) {
//            System.out.println("笔记添加成功");
//            return true;
//        }
//        else
//            System.out.println("笔记添加失败");
//        return false;
        return num == 1;
    }

    @Override
    public List<Note> getNoteByPage(int currPage, int pageSize) {
        /*
         * 通过当前页获取
         * 当前页的起始索引
         */
        int index = (currPage - 1) * pageSize;
        Connection conn = ConnectionUtil.getConnection();
        NoteDao noteDao = new NoteDaoImpl(conn);
        List<Note> noteList = null;
        try {
            noteList = noteDao.selectNoteByPage(index , pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("数据库服务出错了！！！");
        }finally {
            ConnectionUtil.closeALL(null , null , conn);
        }
        return noteList;
    }

    @Override
    public int getNoteCount(){
        Connection conn = ConnectionUtil.getConnection();
        NoteDao noteDao = new NoteDaoImpl(conn);
        int count = 0;
        try {
            count = noteDao.selectNoteCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeALL(null , null , conn);
        }
        return count;
    }

    @Override
    public Note getNoteById(int id){
        Connection conn = ConnectionUtil.getConnection();
        NoteDao noteDao = new NoteDaoImpl(conn);
        Note note = null;
        try {
            note = noteDao.selectNoteById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeALL(null , null , conn);
        }
        return note;
    }
}
